import { defineStore } from "pinia";
import { ref } from "vue";
import type { Ref } from "vue";
import type { ConsultOrderPreParams, Image, PartialConsult, TopDep, ConsultOrderPreData, ConsultOrderItem } from "@/types/consultTypes/consultTypes.ts";
import type { ConsultIllness } from "@/types/consultTypes/consultTypes.ts";
import { ConsultType } from "@/enums";
import { createConsultOrderAPI, fetchConsultOrderDetailAPI, fetchConsultOrderInfoAPI, fetchConsultOrderPayUrlAPI, fetchDepartmentAPI, fetchPrescriptionPicAPI, uploadFileAPI } from "@/api/consultApi/consultApi.ts";

const useConsultStore = defineStore("consultStore", () => {

   const consult: Ref<PartialConsult> = ref<PartialConsult>({});

   const department: Ref<TopDep[]> = ref<TopDep[]>([]);

   const consultOrderInfo: Ref<ConsultOrderPreData> = ref<ConsultOrderPreData>(<ConsultOrderPreData>{});

   const orderId: Ref<string> = ref<string>("");

   const orderDetail:Ref<ConsultOrderItem>=ref<ConsultOrderItem>(<ConsultOrderItem>{})

   // 首页使用-记录问诊类型
   const setType = (type: ConsultType) => {
      consult.value.type = type;
   };

   // 就诊级别页面使用-记录问诊的级别
   const setIllnessType = (type: 0 | 1) => {
      consult.value.illnessType = type;
   };

   // 选择科室页面使用-记录科室id
   const setDepId = (depId: string) => {
      consult.value.depId = depId;
   };

   // 病情描述页面使用-记录患者的病情
   const setIllness = (illness: ConsultIllness) => {
      consult.value.illnessDesc = illness.illnessDesc;
      consult.value.illnessTime = illness.illnessTime;
      consult.value.consultFlag = illness.consultFlag;
      consult.value.pictures = illness.pictures;
   };

   // 患者列表页面使用-记录患者ID
   const setPatientId = (patientId: string) => {
      consult.value.patientId = patientId;
   };

   // 记录优惠券ID
   const setCouponId = (couponId: string) => {
      consult.value.couponId = couponId;
   };

   // 清空问诊记录
   const clearConsult = () => {
      consult.value = {};
   };


   const fetchDepartment = async (): Promise<void> => {
      const { data }: { data: TopDep[] } = await fetchDepartmentAPI();
      department.value = data;
   };

   const uploadFile = async (formData: FormData): Promise<Image> => {
      const { data }: { data: Image } = await uploadFileAPI(formData);
      return data;
   };

   const fetchConsultOrderInfo = async (params: ConsultOrderPreParams): Promise<void> => {
      const { data }: { data: ConsultOrderPreData } = await fetchConsultOrderInfoAPI(params);
      consultOrderInfo.value = data;
   };

   const createConsultOrder = async (dataParams: PartialConsult): Promise<void> => {
      const { data }: { data: { id: string } } = await createConsultOrderAPI(dataParams);
      orderId.value = data.id;
   };

   const fetchConsultOrderPayUrl = async (params: { paymentMethod: 0 | 1, orderId: string, payCallback: string }): Promise<string> => {
      const { data }: { data: { payUrl: string } } = await fetchConsultOrderPayUrlAPI(params);
      return data.payUrl;
   };

   /*获取订单详情*/
   const fetchConsultOrderDetail = async (orderId: string): Promise<void> => {
      const { data }: { data: ConsultOrderItem } = await fetchConsultOrderDetailAPI(orderId);
      orderDetail.value=data
   };

   const fetchPrescriptionPic = async (id:string):Promise<string> => {
      const {data}=await fetchPrescriptionPicAPI(id)
      return  data.url
   }

   return {
      consult,
      department,
      consultOrderInfo,
      orderId,
      orderDetail,
      setType,
      setIllnessType,
      setDepId,
      setIllness,
      setPatientId,
      setCouponId,
      clearConsult,
      fetchDepartment,
      uploadFile,
      fetchConsultOrderInfo,
      createConsultOrder,
      fetchConsultOrderPayUrl,
      fetchConsultOrderDetail,
      fetchPrescriptionPic
   };

}, {
   persist: {
      paths: ["consult"],
   },
});


export default useConsultStore;
